import store from '@/store';
import util from '@/libs/util';
import base64 from '@/libs/base64.js';
import { sendSock } from '../chatwebsocket';
/**
 * 建立微信通道
 */
function deviceAuthRspHandle(msg) {
	store.commit('chatwebsocket/wechat/setWebsocketLogin', true);
}

/**
 * 会话列表推送
 */
function conversationPushNoticeHandle(msg) {
	// store.dispatch('chatwebsocket/wechat/setChatListObj', msg);
	store.dispatch('chatwebsocket/wechat/setFriendList', msg);
}

//  聊天历史记录
function QueryHistoryMessageRespHandle(msg) {
	store.dispatch('chatwebsocket/wechat/setWechatRecord', msg);
}
// 上线
function WeChatOnlineNoticeHandle(msg) {
	let message = JSON.parse(msg.message);
	console.log('上线通知', message);
	store.dispatch('chatwebsocket/wechat/online', message);
	// let deviceAuthReqMsg = {
	// 	Id: 1010,
	// 	AccessToken: '',
	// 	MsgType: 'DeviceAuthReq',
	// 	Content: {
	// 		AuthType: 2,
	// 		Credential: base64.b64EncodeUnicode(message.WeChatId)
	// 	}
	// };
	// sendSock(deviceAuthReqMsg);
	// let wechatInfo = localStorage.getItem('wechatInfo');
	// let arr = wechatInfo.split(',');
	// if (!arr.includes(message.WeChatId)) {
	// 	arr.push(message.WeChatId);
	// }
	// localStorage.setItem('wechatInfo', arr.join(','));
}
// 下线
function WeChatOfflineNoticeHandle(msg) {
	let message = JSON.parse(msg.message);
	console.log('message', message);
	store.commit('chatwebsocket/wechat/wechatOffline', message.WeChatId);
}
/**
 * 获取会话列表
 */
function getWeChatsRspHandle(msg) {
	// console.log(111);
}
/**
 * 好友列表
 */
function friendPushNoticeHandle(msg) {
	// store.dispatch('websocket/websocketWechat/SetHaoList', msg);
}

// 好友发来消息
function FriendTalkNoticeHandle(msg) {
	let message = JSON.parse(msg.message);
	let friendInfo = store.state.chatwebsocket.wechat.currentFriend;
	// 选中当前会话，则刷新聊天记录
	if (friendInfo && friendInfo.wechatid === message.WeChatId && friendInfo.friendid === message.FriendId) {
		// 反馈之后通知已读
		/*let readMsgs = {
			MsgType: 'TriggerMessageReadTask',
			Content: {
				WeChatId: message.WeChatId,
				FriendId: message.FriendId // 好友id
			}
		};
		sendSock(readMsgs);*/

		let msgs = {
			MsgType: 'queryHistoryMessage',
			Content: {
				wechatid: message.WeChatId,
				friendid: message.FriendId, // 好友id
				cid: localStorage.getItem('currentProject'), // 项目id
				page: 1, // 分页页数
				pageSize: 10 // 分页条数
			}
		};
		sendSock(msgs);
	}
	// 1. 更新当前会话的时间和最新消息
	store.commit('chatwebsocket/wechat/updateMessageContentAndTime', util.decode(message));
}
// 我发消息给好友
function TalkToFriendTaskResultNoticeHandle(msg) {
	let friendInfo = store.state.chatwebsocket.wechat.currentFriend;
	let message = JSON.parse(msg.message);
	// 选中当前会话，则刷新聊天记录
	if (friendInfo && friendInfo.wechatid === message.WeChatId && friendInfo.friendid === message.FriendId && message.Success) {
		/*let readMsgs = {
			MsgType: 'TriggerMessageReadTask',
			Content: {
				WeChatId: message.WeChatId,
				FriendId: message.FriendId // 好友id
			}
		};
		sendSock(readMsgs);*/

		setTimeout(() => {
			let msgs = {
				MsgType: 'queryHistoryMessage',
				Content: {
					wechatid: message.WeChatId,
					friendid: message.FriendId, // 好友id
					cid: localStorage.getItem('currentProject'), // 项目id
					page: 1, // 分页页数
					pageSize: 10 // 分页条数
				}
			};
			sendSock(msgs);
			// store.commit('chatwebsocket/wechat/updateSendedContentAndTime', util.decode(message));
		}, 200);
	}
}
// 已读消息
function postMessageReadNoticeHandle(msg) {
	store.dispatch('chatwebsocket/wechat/conversionReaded', msg);
}
// 查看消息详情
function RequestTalkDetailTaskResultNoticeHandle(msg) {
	// console.log('msg123', msg);
	store.dispatch('chatwebsocket/wechat/setDetailTask', msg);
}
// 任务消息结果通知【置顶、取消置顶】
function TaskResultNoticeHandle(msg) {
	store.dispatch('chatwebsocket/wechat/taskResultNotice', msg);
}
export default {
	deviceAuthRspHandle,
	getWeChatsRspHandle,
	conversationPushNoticeHandle,
	friendPushNoticeHandle,
	QueryHistoryMessageRespHandle,
	FriendTalkNoticeHandle,
	TalkToFriendTaskResultNoticeHandle,
	TaskResultNoticeHandle,
	RequestTalkDetailTaskResultNoticeHandle,
	WeChatOnlineNoticeHandle,
	WeChatOfflineNoticeHandle,
	postMessageReadNoticeHandle
};
